package com.hiyee.user.controller;

import com.hiyee.api.BaseController;
import com.hiyee.api.controller.user.UserControllerApi;
import com.hiyee.grace.result.ResponseJsonResult;
import com.hiyee.grace.result.ResponseStatusEnum;
import com.hiyee.pojo.AppUser;
import com.hiyee.pojo.bo.UpdateUserInfoBO;
import com.hiyee.pojo.vo.UserAccountInfoVO;
import com.hiyee.pojo.vo.UserBaseInfoVO;
import com.hiyee.user.service.UserService;
import com.hiyee.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class UserController extends BaseController implements UserControllerApi {

    final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    @Override
    public Object getUserInfo(String userId) {
        // 参数不为空
        if(StringUtils.isBlank(userId)) {
            return ResponseJsonResult.error(ResponseStatusEnum.REQUEST_PARAM_EMPTY);
        }

        // 根据 userId 查询用户信息
        AppUser user = getUser(userId);

        // 组装返回的用户数据
        UserBaseInfoVO userBaseInfoVO = new UserBaseInfoVO();
        BeanUtils.copyProperties(user, userBaseInfoVO);

        return ResponseJsonResult.success(userBaseInfoVO);
    }

    @Override
    public Object getAccountInfo(String userId) {
        // 参数不为空
        if(StringUtils.isBlank(userId)) {
            return ResponseJsonResult.error(ResponseStatusEnum.REQUEST_PARAM_EMPTY);
        }

        // 根据 userId 查询用户信息
        AppUser user = getUser(userId);

        // 组装返回的用户数据
        UserAccountInfoVO userAccountInfoVO = new UserAccountInfoVO();
        BeanUtils.copyProperties(user, userAccountInfoVO);

        return ResponseJsonResult.success(userAccountInfoVO);
    }

    @Override
    public Object updateUserInfo(@RequestBody @Valid UpdateUserInfoBO updateUserInfoBO, BindingResult bindingResult) {
        // 参数验证
        if(bindingResult.hasErrors()) {
            return ResponseJsonResult.errorMap(this.getErrors(bindingResult));
        }
        // 更新用户信息
        userService.updateUser(updateUserInfoBO);

        return ResponseJsonResult.success();
    }

    /**
     * 根据 id 获取用户
     * 优先取缓存
     * @param userId
     * @return
     */
    private AppUser getUser(String userId)
    {
        // 优化系统性能，缓存用户信息到缓存 redis 中
        String redisKey = REDIS_KEY_USER_INFO + userId;
        AppUser user = null;
        if(redisOperator.keyIsExist(redisKey)) {
            user = JsonUtils.jsonToPojo(redisOperator.get(redisKey), AppUser.class);
        } else {
            user = userService.getById(userId);
            redisOperator.set(redisKey, JsonUtils.objectToJson(user));
        }
        return user;
    }
}
